package com.gzdh.dishrecognition.ui.adapter.defaultview;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.ProductConst;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.model.ProductCartModel;
import com.gzdh.dishrecognition.model.WeekMenuBakModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class FoodsAdapter extends RecyclerView.Adapter<FoodsAdapter.MyViewHolder> {

    private List<WeekMenuBakModel.Product> datas = new ArrayList<>();

    private LayoutInflater inflater;

    private Context mContext;

    private OnClick onClick;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void userOnClick(WeekMenuBakModel.Product product);
    }


    public void setDatas(List<WeekMenuBakModel.Product> datas) {
        this.datas = datas;

        notifyDataSetChanged();
    }

    public FoodsAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }


    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_food, null);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv_name.setText(datas.get(position).getName().trim());
        holder.tv_price.setText(new BigDecimal(datas.get(position).getPrice()).toPlainString());
        holder.food_main.setOnClickListener(view -> {
            if (onClick != null) {
                onClick.userOnClick(datas.get(position));
            }
        });
        holder.itemBadge.setVisibility(View.INVISIBLE);
        ProductCartModel productCartModel = ProductConst.getInstance().getProductCartModelList()
                .stream().filter(productCartModel1 -> productCartModel1.getBm().equals(datas.get(position).getBm()))
                .filter(productCartModel1 -> productCartModel1.getZl().equals(datas.get(position).getZl()))
                .filter(productCartModel1 -> productCartModel1.getDinner().equals(datas.get(position).getDinner()))
                .findFirst().orElse(null);
        if (productCartModel != null) {
            holder.itemBadge.setVisibility(View.VISIBLE);
            holder.itemBadge.setText(String.valueOf(productCartModel.getNumber()));
        }
        holder.tv_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, SysConstConfig.getInstance().getFoodsMenuTextSize());
        holder.tv_name.setMaxLines(SysConstConfig.getInstance().getFoodsMenuInternalLine());
        holder.tv_name.setMinLines(SysConstConfig.getInstance().getFoodsMenuInternalLine());
        holder.tv_price.setTextSize(TypedValue.COMPLEX_UNIT_SP, SysConstConfig.getInstance().getFoodsMenuInternalPriceSize());
    }


    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        private TextView tv_price;
        private TextView itemBadge;
        private LinearLayout food_main;


        public MyViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            food_main = itemView.findViewById(R.id.food_main);
            itemBadge = itemView.findViewById(R.id.itemBadge);
        }
    }
}

